package mmi.gui.widget;

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.geom.Point2D;

import javax.swing.BorderFactory;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.BevelBorder;
import javax.swing.border.Border;

import mmi.gui.MainFrame;

public class StatusBar extends JPanel {
	public class CoordinateStatus extends JPanel {
		private static final long serialVersionUID = -5106811101148023371L;
		private JLabel xField = new JLabel("x=0");
		private JLabel yField = new JLabel("y=0");

		public CoordinateStatus() {
			Dimension xFieldDimension = xField.getPreferredSize();
			xField.setPreferredSize(new Dimension(xFieldDimension.width+25, xFieldDimension.height));
			super.add(xField);


			Dimension yFieldDimension = yField.getPreferredSize();
			yField.setPreferredSize(new Dimension(yFieldDimension.width+25, yFieldDimension.height));
			super.add(yField);
		}

		public void setCoordinate(Point2D point)
		{
			xField.setText("x=" + (int) point.getX());
			yField.setText("y=" + (int) point.getY());
		}

	}

	public class WidthStatus extends JPanel {
		private static final long serialVersionUID = -4399207927534584717L;
		private JLabel wField = new JLabel("w=0");
		private JLabel hField = new JLabel("h=0");

		public WidthStatus() {
			Dimension xFieldDimension = wField.getPreferredSize();
			wField.setPreferredSize(new Dimension(xFieldDimension.width+25, xFieldDimension.height));
			super.add(wField);


			Dimension yFieldDimension = hField.getPreferredSize();
			hField.setPreferredSize(new Dimension(yFieldDimension.width+25, yFieldDimension.height));
			super.add(hField);
		}

		public void setWidth(Point2D point)
		{
			wField.setText("w=" + (int) point.getX());
			hField.setText("h=" + (int) point.getY());
		}
	}

	private static final long serialVersionUID = -7446839892751719972L;
	public static Border createComponentBorder()
	{
		return BorderFactory.createCompoundBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED), BorderFactory.createEmptyBorder(0, 4, 0, 4));
	}
	@SuppressWarnings("unused")
	private MainFrame parent = null;

	private final GridBagConstraints gbc = new GridBagConstraints();

	private CoordinateStatus cs = new CoordinateStatus();

	private WidthStatus ws = new WidthStatus();

	public StatusBar(MainFrame parent) {
		super(new GridBagLayout());
		this.parent = parent;
		initialise();
	}

	public void addJComponent(JComponent comp)
	{
		if(comp == null)
		{
			throw new IllegalArgumentException("comp == null");
		}

		comp.setBorder(createComponentBorder());
		super.add(comp, gbc);
	}

	/**
	 * @return the cs
	 */
	public CoordinateStatus getCs() {
		return cs;
	}



	/**
	 * @return the ws
	 */
	public WidthStatus getWs() {
		return ws;
	}

	private void initialise()
	{
		gbc.anchor = GridBagConstraints.WEST;
		gbc.weightx = 0.0;
		gbc.fill = GridBagConstraints.HORIZONTAL;
		gbc.gridy = 0;
		gbc.gridx = 0;
		addJComponent(cs);

		gbc.anchor = GridBagConstraints.WEST;
		gbc.weightx = 0.0;
		gbc.fill = GridBagConstraints.HORIZONTAL;
		gbc.gridy = 0;
		gbc.gridx = 1;
		addJComponent(ws);

		gbc.anchor = GridBagConstraints.WEST;
		gbc.weightx = 1.0;
		gbc.fill = GridBagConstraints.HORIZONTAL;
		gbc.gridy = 0;
		gbc.gridx = 2;
		addJComponent(new JTextField("hola"));
	}

}
